<?php


/**递归重组节点信息为多维数组
 * @param $node 要处理的父级节点数组
 * @param null $access
 * @param int $pid 父级PID
 * @return array
 */
function node_merge($node, $access = null, $pid = 0)
{
    $arr = array();
    foreach ($node as $v) {
        // 判断access是否为数组
        if (is_array($access)) {
            $v['access'] = in_array($v['id'], $access) ? 1 : 0; // 将权限放回到$v数组中
        }
        if ($v['pid'] == $pid) {
            $v['child'] = node_merge($node, $access, $v['id']);
            $arr[] = $v;
        }
    }
    return $arr;
}

/**修改用户识别用户权限函数
 * @param $user_id  用户ID
 * @return array
 */
function user_role($user_id)
{
    $role_user = M('role_user')->order('role_id')->where(array('user_id' => $user_id))->select();
    $role = M('role')->order('id')->select();
    $arr = array();

    foreach ($role as $k => $r) {
        $r['manage'] = 0;
        foreach ($role_user as $ru) {
            if ($ru['role_id'] == $role[$k]['id']) {
                $r['manage'] = 1;
            }
        }
        $arr[] = $r;
    }
    return $arr;
}

/**
 * 判断用户名修改后是否会出现重复
 */
function username_is($uid, $new_username)
{
    $user = D('user')->where(array('id' => $uid))->find();
    $username = $user['username'];
    // 如果修改的用户名跟数据库里查询的用户名不匹配，则进行数据库比较
    if ($new_username != $username) {
        $users = D('user')->where(array('username' => $new_username))->find();
        // 打印SQL输出
        //echo D()->getLastSql();
        if ($users) {
            return true;
        }
    }
    return false;
}

/**
 * 判断角色修改后是否会出现重复名
 */
function rolename_is($id, $new_name)
{
    $role = M('role')->where(array(
        'id' => $id
    ))->find();
    $name = $role['name'];
    if ($new_name != $name) {
        $roles = M('role')->where(array(
            'name' => $new_name
        ))->find();
        //echo M("role")->getLastSql();
        if ($roles) {
            return true;
        }
    }
    return false;
}



/**
+----------------------------------------------------------
 * 功能：计算文件大小
+----------------------------------------------------------
 * @param int $bytes
+----------------------------------------------------------
 * @return string 转换后的字符串
+----------------------------------------------------------
 */
function byteFormat($bytes) {
    $sizetext = array(" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
    return round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . $sizetext[$i];
}



/**
+----------------------------------------------------------
 * 生成随机字符串
+----------------------------------------------------------
 * @param int       $length  要生成的随机字符串长度
 * @param string    $type    随机码类型：0，数字+大写字母；1，数字；2，小写字母；3，大写字母；4，特殊字符；-1，数字+大小写字母+特殊字符
+----------------------------------------------------------
 * @return string
+----------------------------------------------------------
 */
function randCode($length = 5, $type = 0) {
    $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
    $code='';
    if ($type == 0) {
        array_pop($arr);
        $string = implode("", $arr);
    } else if ($type == "-1") {
        $string = implode("", $arr);
    } else {
        $string = $arr[$type];
    }
    $count = strlen($string) - 1;
    for ($i = 0; $i < $length; $i++) {
        $str[$i] = $string[rand(0, $count)];
        $code .= $str[$i];
    }
    return $code;
}


//获得字段类型
function get_field_type($id){
    switch($id){
        case 1:
            return 'int(10)';
            break;
        case 2:
            return 'varchar(255)';
            break;
        case 3:
            return 'text';
            break;
    }
}


//获得input类型
function get_field_input_type($id){
    switch($id){
        case 1:
            return 'text';
            break;
        case 2:
            return 'radio';
            break;
        case 3:
            return 'checkbox';
            break;
        case 4:
            return 'textarea';
            break;
    }
}

//判断一个字符串是否属于序列化后的数据
function is_serialized($data)
{
    $data = trim($data);
    if ('N;' == $data)
        return true;
    if (!preg_match('/^([adObis]):/', $data, $badions))
        return false;
    switch ($badions[1]) {
        case 'a' :
        case 'O' :
        case 's' :
            if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data))
                return true;
            break;
        case 'b' :
        case 'i' :
        case 'd' :
            if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data))
                return true;
            break;
    }
    return false;
}

//添加产品时将 产品模型重组
function fieldshow(){
    $fields = M('FieldProduct')->select();
    foreach($fields as $key=>$val){
        //p(get_field_input_type($val['field_input_type']));
        switch(get_field_input_type($val['field_input_type'])){
            case 'text':
                $fields[$key]['_html'] = "<input type='text' class='lg' name='".$val['field_name']."' />";
                break;
            case 'textarea':
                $fields[$key]['_html'] = "<textarea class='lg' name='".$val['field_name']."'></textarea>";
                break;
            case 'radio':
                //1|开启,0|关闭
                $str = explode(',',$val['field_radio_value']);
                $arr = '';
                foreach($str as $k=>$v){
                    $r = explode('|',$v);
                    $checked =$v[0]==1?'checked':'';
                    $arr .="<input type='radio' name='".$val['field_name']."' value='".$r[1]."' ".$checked.">".$r[1]."&nbsp;&nbsp;&nbsp;&nbsp;";
                }

                $fields[$key]['_html'] = $arr;
                break;

            case 'checkbox':
                //1|X,2|L,3|XL,4|M,5|S
                $str = explode(',',$val['field_checkbox_value']);
                $arr = '';
                foreach($str as $k=>$v){
                    $r = explode('|',$v);
                    $arr .="<input type='checkbox' name='".$val['field_name']."[]' value='".$r[1]."'>".$r[1]."&nbsp;&nbsp;&nbsp;&nbsp;";
                }

                $fields[$key]['_html'] = $arr;
                break;
        }
    }
    return $fields;
}


function edit_fieldshow($data){
        $fields = M('FieldProduct')->select();
        foreach($fields as $key=>$val){
            //p(get_field_input_type($val['field_input_type']));
            switch(get_field_input_type($val['field_input_type'])){
                case 'text':
                    $fields[$key]['_html'] = "<input type='text' class='lg' name='".$val['field_name']."' value='".$data[$val['field_name']]."'/>";
                    break;
                case 'textarea':
                    $fields[$key]['_html'] = "<textarea class='lg' name='".$val['field_name']."'>".$data[$val['field_name']]."</textarea>";
                    break;
                case 'radio':
                    //1|开启,0|关闭
                    $str = explode(',',$val['field_radio_value']);
                    $arr = '';
                    foreach($str as $k=>$v){
                        $r = explode('|',$v);
                        $checked =$r[1]==$data[$val['field_name']]?'checked':'';
                        $arr .="<input type='radio' name='".$val['field_name']."' value='".$r[1]."' ".$checked.">".$r[1]."&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    $fields[$key]['_html'] = $arr;
                    break;
                case 'checkbox':
                    //1|X,2|L,3|XL,4|M,5|S
                    $str = explode(',',$val['field_checkbox_value']);
                    $arr = '';
                    $i=count($data[$val['field_name']]);  //获取当前数据的checkbox 元素值数量
                    foreach($str as $k=>$v){
                        $r = explode('|',$v);
                        $checked ='';
                        for($a=0;$a<$i;$a++){
                            if($r[1]==$data[$val['field_name']][$a]){
                                $checked = 'checked ="checked"';
                            }
                        }
                        $arr .="<input type='checkbox' name='".$val['field_name']."[]' value='".$r[1]."' ".$checked.">".$r[1]."&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    $fields[$key]['_html'] = $arr;
                    break;
            }
        }
        return $fields;
    }

    function _arrayimage($arr){
        $str = implode(',',$arr);
        $var = trim($str);
        $len = strlen($var)-1;
        if($var[$len] == ','){
            $str = substr($str,0,-1);
        }
        $str = explode(',',$str);
        return $str;
    }

